package com.qili.util;

import com.qili.base.entity.CurrentMenu;
import com.qili.base.entity.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import java.util.List;

/**
 * @author liuyang
 * @date 2020/12/8
 * @description
 */
public class AuthUtil {

    /**
     * 判断用户是否有菜单权限
     * @param menuPer       菜单权限
     * @return
     */
    public static boolean checkHaveMenuAuth(String menuPer){
        if(StringUtils.isBlank(menuPer)){
            return false;
        }
        Subject sub = SecurityUtils.getSubject();
        if(null != sub){
            CurrentUser currentUser = (CurrentUser) sub.getPrincipal();
            CurrentUser currentPrincipal = (CurrentUser)sub.getSession().getAttribute("currentPrincipal");
            if(null != currentPrincipal){
                List<CurrentMenu> currentMenuList = currentPrincipal.getCurrentMenuList();
                if(null != currentMenuList && currentMenuList.size()>0){
                    for(CurrentMenu cm : currentMenuList){
                        if(menuPer.equals(cm.getPermission())){
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

}
